﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Practices.Prism.Regions;
using Sautom.ClientComunication;
using Sautom.ClientComunication.Commands;
using Sautom.ClientComunication.Services;
using Sautom.Modules.Authorization.ViewModels;

namespace Sautom.Modules.Authorization.Commands
{
    class ProccessChangePasswordCommand : CommandBase
    {
        #region Fields
        private readonly AuthorizationService _authorizationService;
        private readonly IRegionManager _regionManager;
        private readonly ChangePasswordViewModel _viewModel;
        #endregion

        #region Constructors
        public ProccessChangePasswordCommand(AuthorizationService authorizationService, IRegionManager regionManager, ChangePasswordViewModel changePasswordViewModel)
        {
            _authorizationService = authorizationService;
            _regionManager = regionManager;
            _viewModel = changePasswordViewModel;
        }
        #endregion

        #region Overrides of CommandBase

        public override void Execute()
        {
            if (_authorizationService.ChangePassword(_viewModel.UserName, _viewModel.Password, _viewModel.NewPassword))
            {
                OnSuccess();
            }
            else
            {
                ShowError();
            }
        }

        #endregion

        #region Private members
        private void OnSuccess()
        {
            _regionManager.RequestNavigate(RegionProvider.MainRegion, PathProvider.Login);
        }

        private void ShowError()
        {
            _viewModel.HasError = true;
        }
        #endregion
    }
}
